﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace System
{
    public class RegexHelper
    {
        /// <summary>
        /// 用于验证大陆11位手机号
        /// </summary>
        public static bool MatchMobileNumber_China(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return false;
            }
            Regex regex = new Regex(@"^1[123456789]\d{9}");
            return regex.IsMatch(str);
        }
        /// <summary>
        /// 匹配大陆身份证号码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool MatchIDCard_China(string str)
        {
            /*
             * 身份证首两位代表的地区
             {11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"}
             
              string[] aCity = new string[]{null,null,null,null,null,null,null,null,null,null,null,"北京","天津","河北","山西","内蒙古",null,null,null,null,null,"辽宁","吉林","黑龙江",null,null,null,null,null,null,null,"上海","江苏","浙江","安微","福建","江西","山东",null,null,null,"河南","湖北","湖南","广东","广西","海南",null,null,null,"重庆","四川","贵州","云南","西藏",null,null,null,null,null,null,"陕西","甘肃","青海","宁夏","新疆",null,null,null,null,null,"台湾",null,null,null,null,null,null,null,null,null,"香港","澳门",null,null,null,null,null,null,null,null,"国外"}; 
             */

            Regex regex1 = new Regex(@"^\d{17}(\d|x|X)$");
            if (!regex1.IsMatch(str))
            {
                return false;
            }
            var date = str.Substring(6, 8);
            date = $"{date.Substring(0, 4)}-{date.Substring(4, 2)}-{date.Substring(6, 2)}";
            var isdate = DateTime.TryParse(date, out DateTime dt);
            if (!isdate)
            {
                return false;
            }
            var c = GetIdCardCheckSum(str);
            if (str[17].ToString().ToUpper()[0] == c)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取身份证前17位的校验和
        /// </summary>
        /// <param name="checkSource"></param>
        /// <returns></returns>
        public static char GetIdCardCheckSum(string checkSource)
        {
            if (checkSource.Length != 17 && checkSource.Length != 18)
            {
                throw new Exception("身份证位数不正确，18位的身份证号或其前17位。");
            }
            if (checkSource.Length == 18)
            {
                checkSource = checkSource.Substring(0, 17);
            }
            // 校验表
            int[] check = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            char[] checkBits = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };

            // 校验源转换成数字
            List<int> source = new List<int>();
            for (int i = 0; i < checkSource.Length; i++)
            {
                source.Add(Convert.ToInt32(checkSource.Substring(i, 1)));
            }
            int sum = 0;
            for (int i = 0; i < source.Count; i++)
            {
                sum += source[i] * check[i];
            }
            int remainder = sum % 11;

            var checksum = checkBits[remainder];

            return checksum;

        }
    }
}
